package com.bilicraft.plugin.kexue.utils;

import org.bukkit.Bukkit;

public class ExTimer implements Runnable{
	
	private static ExTimer instance;
	private static Thread thread;

	public long time;
	public int weekday;
	public int hour;
	public int w_h;
	public boolean trace;
	
	public static ExTimer getInstance(){
		if(instance == null){
			instance = new ExTimer();
		}
		return instance;
	}
	
	public static Thread getTimer(){
		if(thread == null || !thread.isAlive()){
			thread = runTimer();
		}
		return thread;
	}
	
	public static Thread runTimer(){
		thread = new Thread(getInstance());
		thread.start();
		return thread;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		Bukkit.getServer().getLogger().info("BiliTimer GOGOGO.");
		while(true){
			if(trace){
				Bukkit.getServer().getLogger().info("Trace time:"+time);
				Bukkit.getServer().getLogger().info("Hour cmds:");
				for(HourCmd hc:BBSConfig.hourCmd){
					if(hc != null){
						for(String s:hc.cmds){
							Bukkit.getServer().getLogger().info("- ["+hc.hour+"]"+s);
						}
					}
				}
				Bukkit.getServer().getLogger().info("Weekday cmds:");
				for(HourCmd hc:BBSConfig.weekCmd.values()){
					for(String s:hc.cmds){
						Bukkit.getServer().getLogger().info("- ["+hc.weekday+"]"+"["+hc.hour+"]"+s);
					}
				}
				hour = (int) ((time/(3600*1000))%24 + BBSConfig.timeZone);
				weekday = (int) ((time/(3600*1000*24) + 4) % 7);
				w_h = weekday*100+hour;
				Bukkit.getServer().getLogger().info("Excuted in this hour:");
				Bukkit.getServer().getLogger().info("+ Last executed:"+BBSConfig.hourCmd[hour].lastRun);
				for(String c:BBSConfig.hourCmd[hour].cmds){
					Bukkit.getServer().getLogger().info("- "+c);
				}
				HourCmd hcc = BBSConfig.weekCmd.get(w_h);
				Bukkit.getServer().getLogger().info("+ Current weekday:"+weekday);
				Bukkit.getServer().getLogger().info("- Mapping:"+w_h);
				if(hcc != null){
					Bukkit.getServer().getLogger().info("+ Last executed:"+hcc.lastRun);
					for(String c:hcc.cmds){
						Bukkit.getServer().getLogger().info("- "+c);
					}
				}else{
					Bukkit.getServer().getLogger().info("- No match.");
				}
				Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "say Console Debug.");
				trace = false;
			}
			time = System.currentTimeMillis();
			try {
				hourAction();
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void hourAction(){
		hour = (int) ((time/(3600*1000))%24 + BBSConfig.timeZone);
		weekday = (int) ((time/(3600*1000*24) + 4) % 7);
		w_h = weekday*100+hour;
		if(time - BBSConfig.hourCmd[hour].lastRun > (24*3600*1000)){
			BBSConfig.hourCmd[hour].lastRun = time;
			Bukkit.getServer().getLogger().info("BiliTimer execute hour Cmds:");
			for(String c:BBSConfig.hourCmd[hour].cmds){
				Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), c);
				Bukkit.getServer().getLogger().info("- "+c);
			}
		}
		HourCmd hc = BBSConfig.weekCmd.get(w_h);
		if(hc != null && time - hc.lastRun > (24*3600*1000)){
			hc.lastRun = time;
			Bukkit.getServer().getLogger().info("BiliTimer execute weekday Cmds:");
			for(String c:hc.cmds){
				Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), c);
				Bukkit.getServer().getLogger().info("- "+c);
			}
		}
	}

}
